package com.r7data.dbstruct.demo;

import java.sql.Connection;
import java.util.List;

import com.r7data.dbstruct.code.DbConstantCode;
import com.r7data.dbstruct.config.DbSource;
import com.r7data.dbstruct.entity.ColumnMetaInfo;
import com.r7data.dbstruct.entity.DbConnectionInfo;
import com.r7data.dbstruct.meta.BaseDbMeta;
import com.r7data.dbstruct.meta.DbMetaFactory;

/**
 * 演示 dbStruct 使用办法
 * @author admin
 *
 */
public class MetaDemo {

	public static void oracleTest(){
		
		String ipAddress = "172.16.1.100";
		int port = 1521;
		String sid = "orcl";
		
		String dbUser = "admin";
		String dbPassword = "admin";
		
		DbConnectionInfo dbInfo = new DbConnectionInfo();
		
		dbInfo.setDbType(DbConstantCode.database_oracle);
		dbInfo.setIpAddress(ipAddress);
		dbInfo.setPort(port);
		dbInfo.setSid(sid);
		
		dbInfo.setDbUser(dbUser);
		dbInfo.setDbPassword(dbPassword);
		
		
		BaseDbMeta dbMeta = null;
		
		Connection con = null;
		
		try {
			dbMeta = DbMetaFactory.getDbMeta(dbInfo);
			con = dbMeta.getDbCon(dbInfo);
			
			List<String> dbList = dbMeta.getDatabases(con);
			
			for(String dbName : dbList){
				System.out.println(dbName);
				
				List<String> tableList = dbMeta.getTables(con, dbName);
				
				for(String tableName : tableList){
					
					List<ColumnMetaInfo> columnList = dbMeta.getColumnInfoList(con, dbName, tableName);
					
					for(ColumnMetaInfo columnInfo : columnList){
						
						String fieldName = columnInfo.getFieldName();
						
						System.out.println(fieldName);
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			DbSource.closeDb(con, null, null);
			
		}
	}
	
	public static void mysqlTest(){
		
		String ipAddress = "localhost";
		int port = 3306;
		String database = "r7cloud";
		
		String dbUser = "root";
		String dbPassword = "admin";
		
		
		DbConnectionInfo dbInfo = new DbConnectionInfo();
		
		dbInfo.setDbType(DbConstantCode.database_mysql);
		dbInfo.setIpAddress(ipAddress);
		dbInfo.setPort(port);
		dbInfo.setDatabase(database);
		
		dbInfo.setDbUser(dbUser);
		dbInfo.setDbPassword(dbPassword);
		
		BaseDbMeta dbMeta = null;
		
		Connection con = null;
		
		try {
			dbMeta = DbMetaFactory.getDbMeta(dbInfo);
			con = dbMeta.getDbCon(dbInfo);
			
			List<String> dbList = dbMeta.getDatabases(con);
			
			for(String dbName : dbList){
				System.out.println(dbName);
				
				List<String> tableList = dbMeta.getTables(con, dbName);
				
				for(String tableName : tableList){
					
					List<ColumnMetaInfo> columnList = dbMeta.getColumnInfoList(con, dbName, tableName);
					
					for(ColumnMetaInfo columnInfo : columnList){
						
						String fieldName = columnInfo.getFieldName();
						
						System.out.println(fieldName);
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			DbSource.closeDb(con, null, null);
			
		}
	}
	
	public static void main(String[] args) {
		mysqlTest();
		
		System.out.println("finished ... ");
	}
	
	
}
